package zbu.adbook.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 */
public class ConnectionManager {
    //驱动类的名字和url地址的前面一般是固定的
    //获得连接的方法，异常被抛出了
    /*public static Connection getCon() throws ClassNotFoundException, SQLException {
        String ClassName ="com.mysql.jdbc.Driver";
        String urlxw = "jdbc:mysql://localhost:3306/addressbooks";
        Class.forName(ClassName);//加载数据库驱动类
        return DriverManager.getConnection(urlxw,"root","123456");//连接addressbooks中的数据库

    }*/
    public static Connection getConnection(){
        Connection con = null;//con的作用域是整个方法
        try {//可能出现的异常
            String ClassName ="com.mysql.jdbc.Driver";
            String urlxw = "jdbc:mysql://localhost:3306/addressbooks";
            Class.forName(ClassName);//加载数据库驱动类
            con =  DriverManager.getConnection(urlxw,"root","123456");//连接addressbooks中的数据库
        } catch (ClassNotFoundException e) {//捕捉异常
            System.out.println(e);
            return null;
        } catch (SQLException e) {//捕捉异常
            System.out.println(e);
            return null;
        }
        return con;
    }
}
